package cn.xfdzx.android.apps.shop.adapter.remake;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.List;

import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.ShopActivity;
import cn.xfdzx.android.apps.shop.bean.MyRemakeListRetailBean;
import cn.xfdzx.android.apps.shop.util.EmptyUtil;
import cn.xfdzx.android.apps.shop.util.ImageGlideUtil;
import cn.xfdzx.android.apps.shop.widget.CornerImageView;

/**
 * Creator: zf.
 * Date: 2020/9/15.
 * Description:订单评价商品列表适配器.-零售我的评价列表
 */
public class OrderMyRemake_RetailAdapter extends BaseQuickAdapter<MyRemakeListRetailBean.DataBean, BaseViewHolder>  {
    private OnItemOnClickListener onItemOnClickListener;
    private List<MyRemakeListRetailBean.DataBean> retailDate;//零售我的评价列表
    private OrderMyRemake_RetailImageListAdapter imageListAdapter;
    public OrderMyRemake_RetailAdapter(@Nullable List<MyRemakeListRetailBean.DataBean> data) {
        super(R.layout.item_myremake_retail, data);
    }

    @Override
    public void setData(int index, @NonNull MyRemakeListRetailBean.DataBean data) {
        super.setData(index, data);
    }

    @Override
    protected void convert(final BaseViewHolder viewHolder, final MyRemakeListRetailBean.DataBean date) {
        RelativeLayout rl_shoptitle = viewHolder.getView(R.id.rl_shoptitle);//店铺布局
        TextView tv_delete = viewHolder.getView(R.id.tv_delete);//删除
        RadioButton tv_goodszan = viewHolder.getView(R.id.tv_goodszan);//点赞商品
        RadioButton tv_goodscai = viewHolder.getView(R.id.tv_goodscai);//踩商品
        TextView tv_remakecontent = viewHolder.getView(R.id.tv_remakecontent);//评价内容
        TextView tv_zl = viewHolder.getView(R.id.tv_zl);//质量
        TextView tv_bz = viewHolder.getView(R.id.tv_bz);//包装
        TextView tv_ps = viewHolder.getView(R.id.tv_ps);//配送

        CornerImageView img_shophead = viewHolder.getView(R.id.img_shophead);
        img_shophead.setType(CornerImageView.TYPE_ROUND);
        CornerImageView img_my = viewHolder.getView(R.id.img_my);
        img_my.setType(CornerImageView.TYPE_CIRCLE);

        ImageView star01 = viewHolder.getView(R.id.star01);
        ImageView star02 = viewHolder.getView(R.id.star02);
        ImageView star03 = viewHolder.getView(R.id.star03);
        ImageView star04 = viewHolder.getView(R.id.star04);
        ImageView star05 = viewHolder.getView(R.id.star05);
        RecyclerView recycler_img = viewHolder.getView(R.id.recycler_img);
        recycler_img.setLayoutManager(new GridLayoutManager(mContext,3));
        if (EmptyUtil.isNotEmpty(date.getReplay().getPics())){
            recycler_img.setVisibility(View.VISIBLE);
            imageListAdapter = new OrderMyRemake_RetailImageListAdapter(date.getReplay().getPics());
            recycler_img.setAdapter(imageListAdapter);
        }else {
            recycler_img.setVisibility(View.GONE);
        }


        if (EmptyUtil.isNotEmpty(date.getReplay().getComment())){
            tv_remakecontent.setVisibility(View.VISIBLE);
            tv_remakecontent.setText(date.getReplay().getComment());
        }else {
            tv_remakecontent.setVisibility(View.GONE);
        }
        ImageGlideUtil.loadImage(mContext,img_shophead,date.getStore().getStore_logo());
        ImageGlideUtil.loadImage(mContext,img_my,date.getUser().getAvatar());
        switch (date.getReplay().getQuality()){
            case "1":
                tv_zl.setText("质量: "+"1星");
                break;
            case "2":
                tv_zl.setText("质量: "+"2星");
                break;
            case "3":
                tv_zl.setText("质量: "+"3星");
                break;
            case "4":
                tv_zl.setText("质量: "+"4星");
                break;
            case "5":
                tv_zl.setText("质量: "+"5星");
                break;

        }
        switch (date.getReplay().getPacking()){
            case "1":
                tv_bz.setText("包装: "+"1星");
                break;
            case "2":
                tv_bz.setText("包装: "+"2星");
                break;
            case "3":
                tv_bz.setText("包装: "+"3星");
                break;
            case "4":
                tv_bz.setText("包装: "+"4星");
                break;
            case "5":
                tv_bz.setText("包装: "+"5星");
                break;
        }
        if (date.getReplay().getRider_evaluate().equals("1")){
            tv_ps.setText("配送: "+"满意");
        }else {
            tv_ps.setText("配送: "+"不满意");
        }
        String zanGoods = " ";
        for (int i = 0; i < date.getGoods().size(); i++) {
            if (date.getGoods().get(i).getIs_fabulous()==1){
                zanGoods = zanGoods+date.getGoods().get(i).getGoods_name()+"，";
            }
        }

        tv_goodszan.setText(zanGoods.substring(0,zanGoods.length()-1));
        clearState(star01,star02,star03,star04,star05,date.getReplay().getService_score());
        viewHolder.setText(R.id.tv_shopname,date.getStore().getStore_name())
                .setText(R.id.tv_mynickname,date.getUser().getNickname())
                .setText(R.id.tv_time,date.getReplay().getCreated_at());

        tv_delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemOnClickListener.onItemOnClick(viewHolder.getLayoutPosition(),date);
            }
        });

        //店铺点击跳转店铺
        rl_shoptitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, ShopActivity.class);
                intent.putExtra("shop_id",date.getStore().getStore_id()+"");

                mContext.startActivity(intent);
            }
        });
    }

    private void clearState(ImageView star01, ImageView star02, ImageView star03, ImageView star04, ImageView star05,String starnum) {
        star01.setVisibility(View.GONE);
        star02.setVisibility(View.GONE);
        star03.setVisibility(View.GONE);
        star04.setVisibility(View.GONE);
        star05.setVisibility(View.GONE);

        switch (starnum){
            case "1":
                star01.setVisibility(View.VISIBLE);
                break;
            case "2":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                break;
            case "3":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                break;
            case "4":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                star04.setVisibility(View.VISIBLE);
                break;
            case "5":
                star01.setVisibility(View.VISIBLE);
                star02.setVisibility(View.VISIBLE);
                star03.setVisibility(View.VISIBLE);
                star04.setVisibility(View.VISIBLE);
                star05.setVisibility(View.VISIBLE);
                break;
        }
    }

    private void setDrawableLeft(TextView tv_cai,Drawable drawable) {
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        tv_cai.setCompoundDrawables(drawable, null, null, null);
    }

    public void clearDatas(){
        mData.clear();
        notifyDataSetChanged();
    }
    public void refreshDatas(List<MyRemakeListRetailBean.DataBean> list){
        super.replaceData(list);
    }

    public void setItemDeleteClickListener(OnItemOnClickListener listener){
        this.onItemOnClickListener = listener;
    }

    public interface OnItemOnClickListener {
        void onItemOnClick(int position, MyRemakeListRetailBean.DataBean date);
    }

}
